﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLKTX1._0.DTO;
using QLKTX1._0.DB;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;
using System.IO;
using System.Web;
using System.Windows.Forms;
namespace QLKTX1._0.DAO
{
    class Service
    {
        public static String saveAndGetFileFromApp(string folder,string nameFile)
        {
            return Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("bin"))+folder+"\\"+nameFile; 
        }

      /*  public static bool checkExistValued(string tableName,string colName,string value)
        {
            string sql = "SELECT COUNT(" + colName + ") as kq FROM " + tableName + " WHERE +" + colName + "="+value ;

            ConnectDB con = new ConnectDB();

            DataTable dt = con.getDataTable(sql);

            if (Int32.Parse(dt.Rows[0]["kq"].ToString())==0){

                return true;
            }

           

            return false;
        }
        */
//-----------------------------------HAM TU DONG TANG KHOA---------------
        public static String generateNoAuto(String tableName, String PKColumnName, String strBegin, int valueBegin)
        {
            String kq = "";

            ConnectDB con = new ConnectDB();

            String sql = "SELECT max(" + PKColumnName + ") as rs FROM " + tableName;//lấy cột có giá trị lớn nhất
            /**ID,100 ( ban dau)
             * ID100 .ID101,ID103->ID103
             * 
             * 
             * 
             * */

            DataTable dt = con.getDataTable(sql);

            if (dt.Rows.Count == 1 && !dt.Rows[0]["rs"].ToString().Equals(""))
            {

                String t = "";

                t = dt.Rows[0]["rs"].ToString(); // t=ID101

                // id100000 -> t=id100000

                int lID=strBegin.Length; // ID=2

                string t2 = t.Substring(lID, t.Length - lID); // (2,6)-> 100000 tach chuoi

                int newNum = Int32.Parse(t2); // int = 100000

                newNum++; //100001

                kq += strBegin + Convert.ToString(newNum);
            }
            else
            { // t,100 -> t100
               // kq=ccbd+String.
                kq = strBegin + Convert.ToString(valueBegin); // iD100
            }
            return kq;
        }
    }
}
